package android_serialport_api;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * <br>������:RootȨ�޲�����
 * <br>������ϸ����:
 * 
 * @author  �Ƴ���
 * @date  [2013-5-23]
 */
public class Get_Root {
	private static Process process = null;
	static boolean is = false;
	private InputStream in = null;
	private String result = null;
	public String RootCmd(String cmd) {
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			in = process.getInputStream();
			byte[] buffer = new byte[256];
			int len = in.read(buffer);
			result = new String(buffer, 0, len);
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}
		return result;
	}

	public static boolean RootCommand()

	{

		try {
			if (Runtime.getRuntime().exec("su").getOutputStream() == null)
				is = false;
			else
				is = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;
	}

	//�ǳ�ţ��һ���ж�һ��Ӧ���Ƿ���Root����Ȩ
	public static boolean hasRoot() {
		char[] arrayOfChar = new char[1024];
		try {
			int j = new InputStreamReader(Runtime.getRuntime().exec("su -c ls")
					.getErrorStream()).read(arrayOfChar);
			if (j == -1) {
				return true;
			}
		} catch (IOException e) {
		}
		return false;
	}
	
	//�����ж��Ƿ���rootȨ��
	public synchronized boolean getRootAhth() {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes("exit\n");
			os.flush();
			int exitValue = process.waitFor();
			if (exitValue == 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
